package com.jack.vipmis.vip.action.service;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.jack.vipmis.common.entity.search.SearchRequest;
import com.jack.vipmis.common.entity.search.Searchable;
import com.jack.vipmis.common.service.BaseService;
import com.jack.vipmis.vip.action.entity.ActionEntity;
import com.jack.vipmis.vip.action.repository.ActionEntityRepository;

/**
 * 
 * @author JackLiang
 * @version 1.0
 * @date 2014-10-9
 */
@Service
public class ActionEntityService extends BaseService<ActionEntity, Long> {

	/**
	 * 
	 * @return
	 */
	private ActionEntityRepository getActionEntityRepository() {
		return (ActionEntityRepository) baseRepository;
	}

	/**
	 * 更新邀请人数量
	 * 
	 * @param number
	 */
	public ActionEntity updateInviters(long id, long number) {
		ActionEntity action = getActionEntityRepository().findOne(id);
		long invitersCount = action.getInviters() + number;
		action.setInviters(invitersCount);

		return getActionEntityRepository().save(action);
	}

	/**
	 * 分页查询
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public Page<ActionEntity> list(int pageNo, int pageSize) {
		Searchable sc = SearchRequest.newSearchable();
		sc.setPage((pageNo - 1), pageSize);
		sc.addSort(Direction.DESC, "createTime");
		return getActionEntityRepository().findAll(sc);
	}
}
